home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / touch.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  77 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  TOUCH.C
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17.  
  18. #ifdef AMIGA
  19. #include <lib/version.h>
  20. #else
  21. #include <include/lib/version.h>
  22. #endif
  23.  
  24. IDENT("touch",".2");
  25. DCOPYRIGHT;
  26.  
  27. main(ac, av)
  28. int ac;
  29. char *av[];
  30. {
  31.     int i;
  32.  
  33.     if (ac == 1) {
  34.         puts(Ident);
  35.         puts(DCopyright);
  36.     printf("touch files/dirs (wildcards ok)\n");
  37.     return(0);
  38.     }
  39.  
  40. #ifndef unix
  41.     expand_args(ac, av, &ac, &av);
  42. #endif
  43.     for (i = 1; i < ac; ++i) {
  44.     char *fn = av[i];
  45.     int fd;
  46.  
  47.     fd = open(fn, O_RDWR);
  48.     if (fd >= 0) {
  49.         char c;
  50.         if (read(fd, &c, 1) == 1) {
  51.         if (lseek(fd, 0L, 0) == 0) {
  52.             write(fd, &c, 1);
  53.             printf("%s touched\n", fn);
  54.         }
  55.         }
  56.         close(fd);
  57.     } else if (errno = ENOFILE) {
  58.         fd = open(fn, O_WRONLY|O_CREAT|O_TRUNC);
  59.         if (fd >= 0) {
  60.         printf("%s created\n", fn);
  61.         close(fd);
  62.         } else {
  63.         char buf[256];
  64.         sprintf(buf, "%s/___touch___", fn);
  65.         fd = open(buf, O_WRONLY|O_CREAT|O_TRUNC);
  66.         if (fd >= 0) {
  67.             close(fd);
  68.             remove(buf);
  69.             printf("directory %s touched\n", fn);
  70.         }
  71.         }
  72.     }
  73.     }
  74.     return(0);
  75. }
  76.  
  77.